package com.taxshare.api.util;

/**
 * @Author wangzhen
 * @Date 2020/4/10 14:11
 * @Version 1.0
 * @Description Junior development engineer
 **/

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * 签名工具
 */
public class SignStringUtil {

    private SignStringUtil() {

    }

    public static final String HMAC_SHA256 = "HmacSHA256";
    public static final String ENCODING = "UTF-8";

    public static String sign(String secret, String string) {
        try {
            Mac hmacSha256 = Mac.getInstance(HMAC_SHA256);
            byte[] keyBytes = secret.getBytes(ENCODING);
            hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HMAC_SHA256));
            return new String(Base64.encodeBase64(hmacSha256.doFinal(string.getBytes(ENCODING))),
                    ENCODING);
        } catch (Exception e) {
            return e.getMessage();
        }
    }

}